#pragma once

#include <string>

namespace solution_008
{

class Solution
{
public:
    int myAtoi(std::string s)
    {
        bool pos = true;
        long long num = 0;

        int i = 0;
        while (i < (int)s.size() && s.at(i) == ' ')
            ++i;
        if (i < (int)s.size()) {
            if (s.at(i) == '-') {
                pos = false;
                ++i;
            } else if (s.at(i) == '+') {
                ++i;
            }
        }

        while (i < (int)s.size() && s.at(i) >= '0' && s.at(i) <= '9') {
            num *= 10;
            num += s.at(i) - '0';

            if (pos && num >= 0x7FFFFFFF)
                return 0x7FFFFFFF;
            if (!pos && num >= 0x80000000)
                return 0x80000000;
            ++i;
        }
        return pos ? num : -num;
    }
};

}  /* namespace solution_008 */
